/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sample;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import rescuecore2.log.Logger;
import rescuecore2.standard.entities.Building;
import rescuecore2.standard.entities.StandardEntity;
import rescuecore2.worldmodel.EntityID;
import rescuecore2.worldmodel.WorldModel;

/**
 * Classe com o id de um determinado obejto / tarefa e o valor de competência
 * @author azul
 */
public class Token {

    private List<GeneralData> data;
    private EntityID sender;
    private int type = 0;

    public Token() {
    	this.data = new LinkedList<GeneralData>();
    }
    
    /**
     * Encode this token to a byte array
     * @return Codified byte array.
     */
    public byte[] encondeToken(WorldModel<? extends StandardEntity> model, EntityID sender, int type) {
    	
    	String token = "";
    	
    	/**
    	 * Types: 1 Burning buildings, 2 Blockades, 3 Victims.
    	 */
    	if (type == 1 || type == 2 || type == 3) {
	    	token += Integer.toString(type) + ":" + Integer.toString(sender.getValue());
	    	
			for (Iterator<GeneralData> it = this.data.iterator(); it.hasNext();) {
					GeneralData next = it.next();
					token += ":" + Integer.toString(next.getID().getValue()) + ":";
					token += Integer.toString(next.getValue());
			}
    	} else if (type == 11) {
    		GeneralData gd = this.data.get(0);
    		token += "F:" + Integer.toString(sender.getValue()) + ":";
    		token += Integer.toString(gd.getID().getValue()) + ":";
    		token += Integer.toString(gd.getValue());
    	}
    	
    	return token.getBytes();
    }
    
    /**
     * Decode a token byte array
     * @param token
     * @return True if a occurs a successful decodification, else return false.
     */
    public int decodeToken(byte[] token, WorldModel<? extends StandardEntity> model) {
    	
    	if (token.length > 0) {
	    	if (token[0] == '1' || token[0] == '2' || token[0] == '3') {
	    		Logger.debug("Msg é um token de incendios ou bloqueios, decodificando...");
	    		String[] param = (new String(token)).split(":");
	    		this.type = Integer.parseInt(param[0]);
	    		this.data = new LinkedList<GeneralData>();
	    		
	    		this.sender = this.getId(Integer.parseInt(param[1]), model);
	    		
	    		for (int i = 2; i < param.length; i = i + 2) {
	    			int id = Integer.parseInt(param[i]);
	    			int value = Integer.parseInt(param[i+1]);
	    			this.data.add(new GeneralData(this.getId(id, model), value));
	    		}
	    	
		        return this.type;
	    	} else if (token[0] == 'F') {
	    		this.type = 11;
	    		Logger.debug("Msg é um token de active task, decodificando...");
	    		this.data = new LinkedList<GeneralData>();
	    		String[] param = (new String(token)).split(":");
	    		this.data = new ArrayList<GeneralData>();
	    		
	    		this.sender = this.getId(Integer.parseInt(param[1]), model);
	    		int id = Integer.parseInt(param[2]);
	    		int pathLength = Integer.parseInt(param[3]);
	    		
	    		GeneralData gd = new GeneralData(this.getId(id, model), pathLength);
	    		this.data.add(gd);
	    		
		        return this.type;
	    	}
	    	else {
	    		Logger.debug("Msg não é um token.");
	    		return 0;
	    	}
    	} else return 0;
    }
    
    private EntityID getId(int id, WorldModel<? extends StandardEntity> model) {
		return model.getEntity(new EntityID(id)).getID();
	}

	public List<GeneralData> getData() {
		return data;
	}

	public void setData(List<GeneralData> data) {
		this.data = data;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public EntityID getSender() {
		return sender;
	}

	public void setSender(EntityID sender) {
		this.sender = sender;
	}
	
}
